package com.yksj.healthtalk.ui.doctorstation;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import org.handmark.pulltorefresh.library.PullToRefreshExpandableListView;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.ui.R;

/**
 * 
 * 医生指南
 * @author origin
 *
 */
public class DocotrStationHelperActivity extends BaseFragmentActivity implements OnClickListener {
	
	PullToRefreshExpandableListView mExpandableListView;
	DataListAdapter mAdapter;
	String type;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_station_helper_layout);
		type = getIntent().getStringExtra("type");
		
		initUI();
		initData();
		
	}
	
	private void initUI(){
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		if(getIntent().hasExtra("title")){
			titleTextV.setText(getIntent().getStringExtra("title"));
		}else{
			titleTextV.setText("患者指南");
		}
		mExpandableListView = (PullToRefreshExpandableListView)findViewById(android.R.id.list);
//		mExpandableListView.setOnItemClickListener(this);
		mExpandableListView.getRefreshableView().setAdapter(mAdapter = new DataListAdapter());
		mExpandableListView.getRefreshableView().setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				return true;
			}
		});
		mExpandableListView.getRefreshableView().setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				JSONObject jsonObject = mAdapter.getChild(groupPosition, childPosition);
				String path = jsonObject.getString("GUIDE_URL_ANDROID");
				path = "http://second-vision.mobi/JumpPage/"+path;
				Intent intent = new Intent(DocotrStationHelperActivity.this,MediaPlayActivity.class);
				intent.putExtra("path",path);
				intent.putExtra("title",jsonObject.getString("GUIDE_NAME"));
				startActivity(intent);
				return false;
			}
		});
		mExpandableListView.setOnRefreshListener(new OnRefreshListener2() {
			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				initData();
			}
			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				
			}
		});
	}
	
	private void initData(){
		HttpRestClient.doHttpQueryDoctorHelper(type,new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, JSONArray response) {
				super.onSuccess(statusCode, response);
				int size = response.size();
				JSONArray mGroupArray = new JSONArray();
				JSONArray mChildArray = new JSONArray();
				for (int i = 0; i < size; i++) {
					JSONObject object = response.getJSONObject(i);
					mGroupArray.add(object);
					mChildArray.add(object.getJSONArray("SUB_GUIDE"));
				}
				mAdapter.onDataChange(mGroupArray, mChildArray);
			}
			@Override
			public void onFinish() {
				super.onFinish();
				mExpandableListView.onRefreshComplete();
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	
	class DataListAdapter extends BaseExpandableListAdapter{
		JSONArray mGroupArray = new JSONArray();
		JSONArray mChildArray = new JSONArray();
		final LayoutInflater mInflater = LayoutInflater.from(getBaseContext());
		 
		private void onDataChange(JSONArray groupArray,JSONArray childArray){
			this.mGroupArray = groupArray;
			this.mChildArray = childArray;
			for (int i = 0; i < mGroupArray.size(); i++) {
				mExpandableListView.getRefreshableView().expandGroup(i);
			}
			notifyDataSetChanged();
		}
		
		@Override
		public int getGroupCount() {
			return mGroupArray.size();
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return mChildArray.getJSONArray(groupPosition).size();
		}

		@Override
		public JSONObject getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			return mGroupArray.getJSONObject(groupPosition);
		}

		@Override
		public JSONObject getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return mChildArray.getJSONArray(groupPosition).getJSONObject(childPosition);
		}

		@Override
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return childPosition;
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = mInflater.inflate(R.layout.dictionary_content_group_title,null);
			}
			JSONObject jsonObject = getGroup(groupPosition);
			((TextView)convertView).setText(jsonObject.getString("GUIDE_NAME"));
			return convertView;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			TextView textView = null;
			if(convertView == null){
				convertView = mInflater.inflate(R.layout.arrow_text_item_layout, null);
				convertView.setTag(R.id.textview1,textView = (TextView)convertView.findViewById(R.id.textview1));
			}else{
				textView = (TextView)convertView.getTag(R.id.textview1);
			}
			JSONObject jsonObject = getChild(groupPosition, childPosition);
			textView.setText(jsonObject.getString("GUIDE_NAME"));
			return convertView;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
	}
	
}
